package leetcode.editor.cn.q1_300.q;

import java.util.Arrays;

/**
 * @author qidi
 * @date 2021-12-06 15:32:45
 */
class Q0048_RotateImage {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public void rotate(int[][] matrix) {
            if (matrix.length == 1) return;
            int size = matrix.length;
            int rows = 0;
            int rowe = size - 1;
            int cols = 0;
            int cole = size - 1;
            while (size > 1) {
                for (int i = 0; i < rowe - rows; i++) {
                    int tmp = matrix[rows][cols + i];
                    matrix[rows][cols + i] = matrix[rowe - i][cols];
                    matrix[rowe - i][cols] = matrix[rowe][cole - i];
                    matrix[rowe][cole - i] = matrix[rows + i][cole];
                    matrix[rows + i][cole] = tmp;
                }

                rows++;
                rowe--;
                cols++;
                cole--;
                size -= 2;
            }
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0048_RotateImage().new Solution();
        int[][] arr = new int[][]{
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9},
        };
        solution.rotate(arr);
        for (int[] ints : arr) {
            System.out.println(Arrays.toString(ints));
        }
        arr = new int[][]{
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16},
        };
        solution.rotate(arr);
        for (int[] ints : arr) {
            System.out.println(Arrays.toString(ints));
        }
    }
}